import frappe


def split_mr_by_default_supplier(doc, method):
    if doc.material_request_type == 'Purchase' and not doc.flags.split_mr:
        item_code_list = {item.item_code for item in doc.items}
        item_default_list = frappe.get_all('Item Default', 
                                        filters = {'company': doc.company,
                                                'parent': ('in', item_code_list)},
                                        fields=['default_supplier', 'parent'])
        supplier_item_map = {}
        for item_default in item_default_list:
            item_list = supplier_item_map.setdefault(item_default.default_supplier, set())
            item_list.add(item_default.parent)

        for i, (supplier, item_list) in enumerate(supplier_item_map.items()):
            if not i:
                first_supplier,first_mr_item_list = (supplier, item_list)
            else:
                new_mr = frappe.copy_doc(doc, ignore_no_copy=False)
                new_mr.zelin_supplier = supplier
                new_mr.flags.split_mr = True
                new_mr.items = [row for row in doc.items if row.item_code in item_list]
                new_mr.insert()
                if doc.docstatus == 1: new_mr.submit()
        doc.zelin_supplier = first_supplier
        doc.items = [row for row in doc.items if row.item_code in first_mr_item_list]

def split_se_by_po_item(doc, method):
    # 满足条件：移动类型为委外调拨，有关联采购订单，没有拆分物料移动自定义标记(避免拆分出来的单据重复执行这段代码)
    if (doc.purpose == "Send to Subcontractor" and doc.items
         and doc.purchase_order and not doc.flags.split_se):
        fg_items = {d.subcontracted_item for d in doc.items}    #获取不重复的委外成品物料清单
        if len(fg_items) > 1:                                   #仅在超过一个委外成品时执行代码  
            for i, fg_item in enumerate(fg_items):
                if not i:
                    first_fg_item = fg_item
                else:                                           #从第二个开始拆分(复制、保留原材料明细行)，新增insert单据
                    new_se = frappe.copy_doc(doc,ignore_no_copy=False)
                    new_se.supplier = doc.supplier              #以下两个字段被设置为不允许复制，这里通过代码复制
                    new_se.from_warehouse = doc.from_warehouse
                    new_se.to_warehouse = doc.to_warehouse
                    new_se.flags.split_se = True                #避免拆分出来的单据运行重复执行这段代码
                    new_se.items =[row for row in doc.items if row.subcontracted_item == fg_item]
                    new_se.insert(ignore_permissions = True)
            #第一个成品仅保留该委外成品所属的下层原材料        
            doc.items = [row for row in doc.items if row.subcontracted_item == first_fg_item]